package com.avic.rong.goodsSort.adapter.secondListAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllTsTimeBean;
import com.avic.rong.search.activity.SearchHomePageActivity;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 */
public class GoodsSortTsTimeShowRvAdapter extends RecyclerView.Adapter<GoodsSortTsTimeShowRvAdapter.GoodsSortTsShowRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllTsTimeBean.DataBean> timeList = new ArrayList<>();

    private GoodsTsTimeRvListener goodsTsTimeRvListener;

    public GoodsTsTimeRvListener getGoodsTsTimeRvListener() {
        return goodsTsTimeRvListener;
    }

    public void setGoodsTsTimeRvListener(GoodsTsTimeRvListener goodsTsTimeRvListener) {
        this.goodsTsTimeRvListener = goodsTsTimeRvListener;
    }

    public GoodsSortTsTimeShowRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setTimeList(List<GoodsSortSearchAllTsTimeBean.DataBean> timeList) {
        this.timeList = timeList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodsSortTsShowRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_goods_sort_ts_rv, viewGroup, false);
        return new GoodsSortTsShowRvViewHolder(view);
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull GoodsSortTsShowRvViewHolder holder, int i) {

        //设置标题筛选高亮
//        if (timeList != null){
//            SpannableString spannableString = new SpannableString(timeList.get(i).getTitle());
//            int startIndex = timeList.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
//            if (startIndex>=0){
//                ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
//                spannableString.setSpan(colorSpan, startIndex, startIndex+SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
//                holder.title.setText(spannableString);
//            }else {
                holder.title.setText(timeList.get(i).getTitle());
//            }
//        }
        DecimalFormat df=new DecimalFormat("###0.00");
        String format = df.format(timeList.get(i).getPrice());

        if (StringUtils.isEmpty(timeList.get(i).getImg())){
            holder.layout.setVisibility(View.VISIBLE);
            holder.img.setVisibility(View.GONE);
            if (StringUtils.isEmpty(timeList.get(i).getSeries())){
                holder.down.setText(timeList.get(i).getSeries());
            }
            holder.up.setText(timeList.get(i).getNo());
        }else{
            holder.layout.setVisibility(View.GONE);
            Glide.with(context).load(timeList.get(i).getImg()).placeholder(R.mipmap.doc_default).into(holder.img);
        }
        holder.desc.setText(timeList.get(i).getDesc() + "");
        holder.price.setText("¥" + format);
        holder.car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = timeList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx","splite:"+split[2]);
                String skuStr = "{"+"\""+split[2].toString()+"\""+":"+"1}";
                goodsTsTimeRvListener.addTimeTsCar(skuStr);
            }
        });
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = timeList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);

                Intent intent = DetailProductActivity.getIntent(context, split[2]);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return timeList.size();
    }

    class GoodsSortTsShowRvViewHolder extends RecyclerView.ViewHolder {
        TextView title, desc, price;
        ImageView car, img;
        LinearLayout linearLayout;
        TextView down,up;
        LinearLayout layout;
        public GoodsSortTsShowRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.goods_sort_ts_rv_img);
            price = itemView.findViewById(R.id.goods_sort_ts_rv_price);
            title = itemView.findViewById(R.id.goods_sort_ts_rv_title);
            car = itemView.findViewById(R.id.goods_sort_ts_rv_car);
            desc = itemView.findViewById(R.id.goods_sort_ts_rv_desc);
            linearLayout = itemView.findViewById(R.id.goods_sort_ts_LinearLayout);
            layout = itemView.findViewById(R.id.goods_wd_text_layout);
            down = itemView.findViewById(R.id.goods_wd_text_down);
            up = itemView.findViewById(R.id.goods_wd_text_up);
        }
    }
    public interface GoodsTsTimeRvListener{
        void addTimeTsCar(String skuStr);
    }
}
